import sys


sys.path.append("../")
from py.bcosclient_utils import *;



# Create your views here.
from django.http import HttpResponse
from django.http import HttpRequest
import json

from django.views.decorators.csrf import csrf_exempt


# Create your views here.

def setResponseHeader(response,origin):
  response["content-type"]="text/json"
  response['Acess-Control-Allow-Origin']=origin
  return response

def getInfo(input):
  if type(input) is bytes:
    input = str(input,"UTF-8")
  print("input",input)
  info = {"name":"bcos3 lite js 测试服务","version":"1.0.0","input":input}
  return info

#简单的吐一个返回信息
def info(request:HttpRequest):
  origin = "*"
  #origin = request.headers['Origin']
  if request.method == 'POST':
    name = request.POST.get("name")
    print("request.body:",request.body)
    input  = request.body
    paramlist = request.POST.getlist("key")
    print("paramlist:",paramlist)
  else:
    name = request.GET.get("name")
    print(request.GET)
    input = request.GET
    paramlist = request.GET.getlist("key")
    print("GET paramlist:",paramlist)
  infores = getInfo(input)
  resptext = json.dumps(infores,indent=4)
  print("response:",resptext)
  response =  HttpResponse(resptext)
  response = setResponseHeader(response,origin)
  print(response.headers)
  return response




#web服务的rpc入口
def rpc(request:HttpRequest):
  origin = "*"
  if request.method == 'POST':
    requestSource= request.POST
  else:
    requestSource = request.GET
  cmd = requestSource.get("cmd")
  paramlist=requestSource.getlist("params")
  print(f"cmd = {cmd},paramlist={paramlist}")
  result = call_rpc(bcos3clent,cmd,paramlist)
  resptext = json.dumps(result,indent=4)
  response = HttpResponse(resptext)
  response = setResponseHeader(response,origin)
  return response



#web服务命令行式调用合约的接口
def contract(request:HttpRequest):
  'sendtx/call name address params'

  origin = "*"
  if request.method == 'POST':
    requestSource= request.POST
  else:
    requestSource = request.GET
  cmd =   requestSource.get("cmd")
  name =  requestSource.get("name")
  address = requestSource.get("address")
  fn_name = requestSource.get("fn")
  params = requestSource.getlist("params")
  if cmd=='call':
    result = call(bcos3clent,name,address,fn_name,params)
  if cmd=='sendtx':
    result = sendtx(bcos3clent, name, address, fn_name, params)
  resptext = json.dumps(result, indent=4)
  response = HttpResponse(resptext)
  response = setResponseHeader(response, origin)
  return response

#web服务传入rawdata，调用合约的接口，相当于简单转发一下rawdata给链，做一些返回的解析
def raw(request:HttpRequest):
  origin = "*"
  if request.method == 'POST':
    requestSource= request.POST
  else:
    requestSource = request.GET
  cmd =   requestSource.get("cmd")
  abistr = requestSource.get("abi")
  name = requestSource.get("name")
  address = requestSource.get("address")
  rawdata = requestSource.get("rawdata")
  print("rawdata is ",rawdata)
  if cmd == "call":
    result = callraw(bcos3clent,address,rawdata,abistr)
  if cmd == "sendtx":
    result = bcos3clent.sendRawTransactionData(rawdata)
    if abistr is not None:
      abiparser = DatatypeParser()
      abiparser.from_text(abistr)
      result = parse_tx_result(abiparser,result)
  resptext = json.dumps(result, indent=4)
  response = HttpResponse(resptext)
  response = setResponseHeader(response, origin)
  return response
      
    
  